<?php
/**
 * register.php
 * 
 * This file contains the code for the Register webpage and the code for validating a user's registration.
 */
error_reporting (E_ALL ^ E_NOTICE);
require_once 'interface.php';
webpageDoctype();
print_html_title("Member System - Register");
webpageMetaAndBodyStart();
echo "<p><div align='center'>";
echo "<h2>Register</h2><br/>";

if ($_POST['registerbtn'] === "Register"){
	$getuser = $_POST['user'];
	$getemail = $_POST['email'];
	$getpass = $_POST['pass'];
	$getretypepass = $_POST['retypepass'];
	
	if(strlen($getuser) > 0) {
			if(strlen($getpass) > 0) {
				if(strlen($getretypepass) > 0) {
					if($getpass === $getretypepass){
						require("./connect.php");
						
						$query = mysql_query("SELECT * FROM users WHERE username='$getuser'");
						$numrows = mysql_num_rows($query);
						// If $numrows equals 0 no users are using the username being registered.
						if($numrows == 0){
								$date = time();
								$code = md5(rand());
								
								mysql_query("INSERT INTO users "
										. "(username, password, email, preferences, code, active, date) "
										. "VALUES "
										. "('$getuser', '$getpass', '', '0', '$code', '1', '$date')");
								
								$query = mysql_query("SELECT * FROM users WHERE username='$getuser'");
								$userid = mysql_insert_id();
								$numrows = mysql_num_rows($query);
								if($numrows == 1){
									$_SESSION['userid'] = $userid;
									$_SESSION['username'] = $getuser;
									echo <<<EOL
									<meta http-equiv="Refresh" content="10; url=userpreference.php" />
									<h3>
									Thank you for registering for a HyperLinks user account.<br/>
									You will shortly be redirected to the User Preferences page to select your user preferences and 
									activate your HyperLinks user account.<br/><br/>
									Click <a href="userpreference.php">here</a> to redirect the User Preferences page now.
									</h3>
EOL;
									return;
								}else{
									$errormsg= "An error has occured. Your account was not created.";
								}
						}else{
							$errormsg = "There is already a user with that username";
						}
						mysql_close($con);
					}else{
						$errormsg = "Your passwords did not match.";
					}
				}else{
					$errormsg ="You must retype your password to register.";
				}
			}else{
				$errormsg ="You must enter your password to register.";
			}
	}else{
		$errormsg = "You must enter your username to register.";
	}
}

$form = "<form action ='./register.php' method='post'>
<table>
<tr>
	<td></td>
	<td><font color ='red'>$errormsg</font></td>
</tr>
<tr>
	<td>Username:</td>
	<td><input type='text' name='user' value='$getuser' /></td>
</tr>
<tr>
	<td>Password:</td>
	<td><input type='password' name='pass' value='' /></td>
</tr>
<tr>
	<td>Retype:</td>
	<td><input type='password' name='retypepass' value='' /></td>
</tr>
<tr>
	<td></td>
	<td><input type='submit' name='registerbtn' value='Register' /></td>
</tr>
</table>
</form>";

echo $form;
echo "</div></p>";
webpageFooter();
?>